package com.hemaapp.yjnh.activity;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomListView;
import xtom.frame.view.XtomRefreshLoadmoreLayout;
import xtom.frame.view.XtomRefreshLoadmoreLayout.OnStartListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.hm_FrameWork.view.RefreshLoadmoreLayout;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.AfterSalesAdapter;
import com.hemaapp.yjnh.bean.AfterSale;
import com.hemaapp.yjnh.bean.EventBusConfig;
import com.hemaapp.yjnh.bean.EventBusMsg;
import com.hemaapp.yjnh.bean.User;

/**售后列表
 * @author ZhangGuiyou
 * Date:2015-09-10 13:34
 */
public class AfterSalesListActivity extends BaseActivity {

	public static final int REQUEST_MOD = 0x01;
	
	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;
	
	private RefreshLoadmoreLayout parentLayout;
	private XtomListView listview;
	
	private User user;
	private AfterSalesAdapter adapter;
	private ArrayList<AfterSale> afterSales = new ArrayList<AfterSale>();
	private int page = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_aftersales);
		user = getApplicationContext().getUser();
		super.onCreate(savedInstanceState);
		EventBus.getDefault().register(this);
		getAfterSalesList();
	}

	@Override
	public void onDestroy() {
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}
	
	private void getAfterSalesList() {
		if (user != null) {
			getNetWorker().afterSalesList(user.getToken(), page+"");
		}
	}
	
	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case SALE_SUPPORT_LIST:
			showProgressDialog("请稍候");
			break;
		default:
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case SALE_SUPPORT_LIST:
			cancelProgressDialog();
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case SALE_SUPPORT_LIST:
			String page = netTask.getParams().get("page");
			HemaPageArrayResult<AfterSale> aResult = (HemaPageArrayResult<AfterSale>) baseResult;
			if ("0".equals(page)) {// 刷新
				parentLayout.refreshSuccess();
				afterSales.clear();
				afterSales.addAll(aResult.getObjects());
			} else {// 更多
				parentLayout.loadmoreSuccess();
				if (aResult.getObjects().size() > 0) {
					afterSales.addAll(aResult.getObjects());
				} else {
					parentLayout.setLoadmoreable(false);
					XtomToastUtil.showShortToast(mContext, "已经到最后啦");
				}
			}
			refreshData(1);
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case SALE_SUPPORT_LIST:
			String page = netTask.getParams().get("page");
			if ("0".equals(page)) {// 刷新
				parentLayout.refreshFailed();
			} else {// 更多
				parentLayout.loadmoreFailed();
			}
			showTextDialog(baseResult.getMsg());
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case SALE_SUPPORT_LIST:
			String page = netTask.getParams().get("page");
			if ("0".equals(page)) {// 刷新
				parentLayout.refreshFailed();
			} else {// 更多
				parentLayout.loadmoreFailed();
			}
			refreshData(failedType);
			break;
		default:
			break;
		}
	}
	
	private void refreshData(int failtype) {
		if (null == adapter) {
			adapter = new AfterSalesAdapter(mContext, afterSales);
			adapter.setEmptyString("暂无售后记录");
			listview.setAdapter(adapter);
		}else {
			adapter.setEmptyString("暂无售后记录");
			adapter.notifyDataSetChanged();
		}
		adapter.setFailtype(failtype);
	}

	@Override
	protected void findView() {
		titleText = (TextView) findViewById(R.id.title_text);
		titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
		titleRight = (Button) findViewById(R.id.title_right_btn);

		parentLayout = (RefreshLoadmoreLayout) findViewById(R.id.refreshLoadmoreLayout);
		listview = (XtomListView) findViewById(R.id.listview);
	}

	@Override
	protected void getExras() {
		
	}

	@Override
	protected void setListener() {
		titleText.setText("售后");
		titleLeft.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleRight.setVisibility(View.GONE);
		parentLayout.setVisibility(View.VISIBLE);
		parentLayout.setOnStartListener(new StartListener());
	}
	
	private class StartListener implements OnStartListener {

		@Override
		public void onStartRefresh(XtomRefreshLoadmoreLayout v) {
			page=0;
			getAfterSalesList();
		}

		@Override
		public void onStartLoadmore(XtomRefreshLoadmoreLayout v) {
			page++;
			getAfterSalesList();
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK) {
			return;
		}
		switch (requestCode) {
		case REQUEST_MOD:
			page=0;
			getAfterSalesList();
			break;
		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Subscribe
	public void onEventMainThread(EventBusMsg msg) {
		if (msg.getType() == EventBusConfig.UPDATE_ORDER) {
			page = 0;
            getAfterSalesList();
		}
	}
}
